package ge.telasi.tasks.ui.search;

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author dimitri
 */
public class TextSearch extends JPanel implements FieldEditor<TextField> {
    private JComboBox combo;
    private JTextField text;
    private TextField proto;

    public TextSearch() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new BorderLayout(5, 5));
        combo = new JComboBox(new Integer[] {
            TextField.EXACT, TextField.START, TextField.END, TextField.CONTAINS
        });
        combo.setRenderer(new MethodRenderer());
        text = new JTextField();
        add(combo, BorderLayout.WEST);
        add(text, BorderLayout.CENTER);
    }

    public void setField(TextField field) {
        this.proto = field;
    }

    public TextField getField() {
        TextField field = new TextField(proto.getName(), proto.getHumanName());
        field.setMethod((Integer) combo.getSelectedItem());
        field.setText(text.getText());
        return field;
    }

    private class MethodRenderer extends DefaultListCellRenderer {
        
        MethodRenderer() {
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Integer) {
                int method = (Integer) value;
                if (method == TextField.EXACT) {
                    setText("ზუსტად");
                } else if (method == TextField.START) {
                    setText("იწყება");
                } else if (method == TextField.END) {
                    setText("ბოლოვდება");
                } else if (method == TextField.CONTAINS) {
                    setText("შეიცავს");
                }
            }
            return this;
        }

    }

}
